<?php

namespace api\controllers;

use Yii;
use common\models\Agent;
use common\models\Carousel;
use common\models\Information;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * AgentController implements the CRUD actions for Agent model.
 */
class ApiController extends Controller
{
    //人员代理API
    public function actionAgent(){
        $agent = Agent::findBySql('SELECT id,nickname FROM Agent')->all();
        $ret=[];
        //$ret1=[];
        foreach($agent as $v){
            $re['data'][]= [
                'id' => $v->id,
                'name' => $v->nickname
            ];
        }
        return json_encode($re);
    }
    //轮播API
    public function actionCarousel(){
        $agent = Carousel::findBySql('SELECT img,link FROM carousel')->all();
        $ret=[];
        foreach($agent as $v){
            $ret['data'][]= [
                'img' => $v->img,
                'link' => $v->link,
            ];
        }
        return $this->asJson($ret);
    }
    //资讯API
    public function actionInformation(){
        $agent = Information::findBySql('SELECT * FROM information')->all();
        $ret=[];
        foreach($agent as $v){
            $ret['data'][]= [
                'id' => $v->id,
                'cover_img' => $v->cover_img,
                'content' => $v->content,
                'title' => $v->title,
                'link' => $v->link,
                'commentcount' => $v->commentcount,
                'browsecount' => $v->browsecount,
                'created_at' => $v->created_at,
            ];
        }
        return json_encode($ret);
    }
    //资讯详情内容API
    public function actionInformationcontent($id){
        $agent = Information::findBySql('SELECT * FROM information WHERE id='.$id)->all();
        $ret=[];
        foreach($agent as $v){
            $ret['data'][]= [
                'id' => $v->id,
                'content' => $v->content,
                'title' => $v->title,
                'commentcount' => $v->commentcount,
                'browsecount' => $v->browsecount,
                'created_at' => $v->created_at,
            ];
        }
        return json_encode($ret);
    }





}
